/**
 * 部门树形下拉菜单
 * Created by wuwt on 2015/7/28.
 * 使用说明:
 js: $(function(){
            $("#department_name").();
       })
 html:
 <div class="form-group">
 <label class="col-sm-2 control-label">部门：</label>
 <div class="col-sm-4">
 <input class="form-control" type="text" id="department_name" name="departmentName" ztree="department" readonly>
 <input type="hidden" id="department_id" name="departmentId">
 </div>
 </div>
 *
 */
;(function($) {
    $.fn.departmentTreeMenu = function(){
        var input = $(this).attr("ztree");
        var treeMenu = input+"_departmentTreeMenu";
        var treeMenuDiv = input+"_departmentTreeMenu_div";
        var div = '<div id="'+treeMenuDiv+'" class="ztreemenu" style="display:none; position: absolute;z-index: 1888;">';
        div+='<ul id="'+treeMenu+'" class="ztree" style="margin-top:0;height:250px;overflow: auto;"></ul></div>';
        $("#"+input+"_id").after(div);
        var setting = {
            view: {
                dblClickExpand:false
            },
            data:{
                simpleData:{
                    enable: true
                }
            },
            callback: {
                onClick: function(e, treeId, treeNode){
                    var zTree = $.fn.zTree.getZTreeObj(treeId);
                    var nodes = zTree.getSelectedNodes();
                    var nodeId = nodes[0].id;
                    var nodeName = nodes[0].name;
                    $("#"+input+"_id").val(nodes[0].id);
                    $("#"+input+"_name").val(nodes[0].name);
                    hideMenu();
                    zTree.cancelSelectedNode();//取消节点选中状态
                }
            }
        };
        //加载部门树形节点
        $.post("/osm/controller/department/getDepartmentList",function(data){
            var departmentList = data.departmentList;
            var companyName = data.companyName;
            var zNodes = "[{ id:'', pId:'', name:companyName, path:'', open:true, noRemoveBtn: true, noRenameBtn: true}";
            $.each(departmentList,function(i,dept){
                var id = dept.departmentId;
                var pId = dept.parentId==''?'0':dept.parentId;
                var name = dept.departmentName;
                var pName = dept.parentName==''?companyName:dept.parentName;
                var deptNum = dept.departmentNum;
                var orderNum = dept.orderNum;
                var path = dept.path;
                zNodes+=",{id: '"+id+"', pId: '"+pId+"', name: '"+name+"', pName: '"+pName+"', deptNum: '"+deptNum+"', orderNum: "+orderNum+", path: '"+path+"', open: true}";
            });
            zNodes+="]";
            $.fn.zTree.init($("#"+treeMenu), setting, eval(zNodes));
        });
        var hideMenu = function(){
            $("#"+treeMenuDiv).fadeOut("fast");
            $("body").unbind("mousedown", onBodyDown);
        };
        var onBodyDown = function(event){
            if (!(event.target.id == "menuBtn" || event.target.id == treeMenuDiv || $(event.target).parents("#"+treeMenuDiv).length>0)) {
                hideMenu();
            }
        };
        $(this).click(function(){
            $("#"+treeMenuDiv).css({left:15+"px", top:30 + "px",width:$(this).width()+26+"px"}).slideDown("fast");
            $("body").bind("mousedown", onBodyDown);
        })
    };
})(jQuery);